package cn.swing.his.passport.service.impl;

import cn.swing.his.common.exception.HisServiceException;
import cn.swing.his.common.web.ResponseCode;
import cn.swing.his.passport.mapper.DoctorMapper;
import cn.swing.his.passport.mapper.DoctorPermissionMapper;
import cn.swing.his.passport.pojo.domain.DoctorAutority;
import cn.swing.his.passport.pojo.domain.DoctorDetails;
import cn.swing.his.pojo.password.vo.DoctorLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DoctorDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private DoctorPermissionMapper doctorPermissionMapper;
    @Autowired
    private DoctorMapper doctorMapper;

    // 获取用户的信息
    @Override
    public UserDetails loadUserByUsername(String telephone) throws UsernameNotFoundException {
        DoctorLoginVO doctorInfo = doctorMapper.findDoctorInfoByTelephone(telephone);
        if (doctorInfo == null) {
            throw new HisServiceException(ResponseCode.ERR_TELEPHONE_NOT_FOUND,"登录失败,电话号码不正确");
        }
        // 检查账号是否被禁用
        if(doctorInfo.getIsEnable() == 0){
            throw new HisServiceException(ResponseCode.ACCOUNT_IS_DISABLED,"登录失败,用户账号已经被禁用");
        }
        // 获取该医生的权限列表
        List<String> doctorLoginPermissions = doctorPermissionMapper.getDoctorLoginPermissions(doctorInfo.getId());
        log.info("该医生的权限有doctorLoginPermissions={}",doctorLoginPermissions);
        // 封装权限信息
        List<DoctorAutority> authorities = new ArrayList<>();
        for (String doctorLoginPermission : doctorLoginPermissions) {
            DoctorAutority doctorAutority = new DoctorAutority();
            doctorAutority.setName(doctorLoginPermission);
            authorities.add(doctorAutority);
        }
        log.info("封装后的权限信息有authorities={}",authorities);
        // 将查询到的医生信息转换成UserDetails类型
        DoctorDetails doctorDetails = new DoctorDetails();
        BeanUtils.copyProperties(doctorInfo,doctorDetails);
        doctorDetails.setAuthorities(authorities);
        log.info("doctorDetails={}",doctorDetails);
        return doctorDetails;
//        UserDetails doctorDetails = User.builder()
//                .username(doctorInfo.getTelephone())
//                .password(doctorInfo.getPassword())
//                .accountExpired(false)
//                .accountLocked(false)
//                .disabled(false)
//                .credentialsExpired(false)
//                .build();
//        log.info("转换得到UserDetails={}",doctorDetails);
//        return doctorDetails;
    }
}
